package soulkun.library.mvvm.component

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import soulkun.library.component.BaseFragment
import soulkun.library.utils.log.LogUtils
import java.lang.IllegalStateException
import java.lang.NullPointerException

abstract class BaseDataBindingFragment<Binding: ViewDataBinding>(private val isNeedLogLifecycle: Boolean = false) : Fragment() {

    companion object{
        private const val TAG = "BaseDataBindingFragment"
    }

    private var mBinding: Binding? = null
    protected lateinit var mContext: Context
    protected lateinit var mActivity: AppCompatActivity
    protected lateinit var mFragment: Fragment
    protected lateinit var mActivityFragmentManager: FragmentManager

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onAttach")
        mActivity = activity as AppCompatActivity
        mActivityFragmentManager = mActivity.supportFragmentManager
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onCreate")
        super.onCreate(savedInstanceState)
        mFragment = this
        doOnCreate()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onCreateView")
        mBinding = DataBindingUtil.inflate(inflater, setLayout(), container, false)
        mBinding!!.lifecycleOwner = viewLifecycleOwner
        return mBinding!!.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onViewCreated")
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
        initListener()
    }

    override fun onStart() {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onStart")
        super.onStart()
    }

    override fun onResume() {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onResume")
        super.onResume()
    }

    override fun onPause() {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onPause")
        super.onPause()
    }

    override fun onStop() {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onStop")
        super.onStop()
    }

    override fun onDestroyView() {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onDestroyView")
        super.onDestroyView()
        mBinding = null
    }

    override fun onDestroy() {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onDestroy")
        super.onDestroy()
    }

    override fun onDetach() {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onDetach")
        super.onDetach()
    }

    fun requireDataBinding(): Binding {
        return mBinding?: throw NullPointerException("mBinding is null")
    }


    @LayoutRes
    protected abstract fun setLayout(): Int

    protected abstract fun initView()

    protected abstract fun initData()

    protected abstract fun initListener()

    protected abstract fun doOnCreate()


}